/************************************************************\
	AJAX Library v0.93
	@author: Ignacio X. Domínguez
	http://ajax.ignacioxd.com.ve/
\************************************************************/

function getXmlHttpObject() {
	var xmlHttp = false;
	if(window.XMLHttpRequest)
		xmlHttp = new XMLHttpRequest();
	else if(window.ActiveXObject) {
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlHttp = false;
			}
		}
		@end @*/
	}
	return xmlHttp;
}

function Ajax(name) {
	var ajax = new Object();
	ajax.prototype = this;
	/********************\
	*  PUBLIC VARIABLES  *
	\********************/
	ajax.name = "AJAX Object";
	if(arguments.length != 0) ajax.name += " - " + name;

	ajax.async = true;	
	ajax.username = null;
	ajax.password = null;
	ajax.loadingElementID = "";
	/*********************\
	*  PRIVATE VARIABLES  *
	\*********************/
	ajax.isBusy = false;
	ajax.method = "GET";
	ajax.resultElement = "";
	ajax.xmlHttp = getXmlHttpObject();
	if(!ajax.xmlHttp) return null;
	
	/*************************\
	*  PUBLIC EVENT HANDLERS  *
	\*************************/	
	ajax.onTimeout = null; //Planned for future versions
	ajax.onLoading = null;
	ajax.onLoaded = null;
	ajax.onInteractive = null;
	ajax.onResponse = null;
	ajax.onResponseDOM = null;
	ajax.onDone = null;
	ajax.onError = function(err) {};
	
	
	ajax.onResponseDefault = function(responseText) {
		if(ajax.resultElement!=null && ajax.resultElement!="")
			if(typeof(ajax.resultElement) == "object")
				obj = ajax.resultElement;
			else
				obj = document.getElementById(ajax.resultElement);
		else
			return;
		if(typeof(obj) != 'undefined') {
			if(obj.tagName == "INPUT" || obj.tagName == "TEXTAREA") {
				obj.value = responseText;
			}
			else if(obj.tagName == "SELECT") {
				try {
					array = eval(responseText);
					if(array.constructor.toString().indexOf("Array") != -1) { //Fill combo
						for(m=obj.options.length-1; m>-1; m--)
							obj.options[m]=null;
						for(i=0; i<array.length; i++){
							if(array[i] != "")
								obj.options[i] = new Option(array[i][0],array[i][1]);
						}
						//obj.options[0].selected=true;
					}
					else
						obj.value = responseText;
				}
				catch(ex) {
					obj.value = responseText;
				}
			}
			else
				obj.innerHTML = responseText;
		}
		else ajax.onError(2); //No se encontro el elemento a cambiar
	};
	
	/** readyState **\
	0 = uninitialized
	1 = loading
	2 = loaded
	3 = interactive
	4 = complete
	\****************/	
	ajax.onStateChange = function() {
		if(ajax.xmlHttp.readyState==1 && typeof(ajax.onLoading) == "function") ajax.onLoading();
		if(ajax.xmlHttp.readyState==2 && typeof(ajax.onLoaded) == "function") ajax.onLoaded();
		if(ajax.xmlHttp.readyState==3 && typeof(ajax.onInteractive) == "function") ajax.onInteractive();
		if(ajax.xmlHttp.readyState==4) {
			if(ajax.xmlHttp.status==200) {
				if(typeof(ajax.onResponse) == "function") ajax.onResponse(ajax.xmlHttp.responseText);
				else ajax.onResponseDefault(ajax.xmlHttp.responseText);
				if(typeof(ajax.onResponseDOM) == "function") ajax.onResponseDOM(ajax.xmlHttp.responseXML);
			}
			else
				ajax.onError(ajax.xmlHttp.status); //Error en el request
			ajax.isBusy = false;
			ajax.hideLoadingElement();
			if(typeof(ajax.onDone) == "function") ajax.onDone();
		}
	};
	
	/******************\
	*  PUBLIC METHODS  *
	\******************/
	ajax.cancelRequest = function() {
		ajax.xmlHttp.abort();
	};
	
	ajax.doRequest = function() {
		if(ajax.isBusy) {
			//setTimeout("ajax.doRequest('"+URL+"', "+async+", '"+method+"', '"+id+"', '"+args+"');", 500);
		}
		else {
			ajax.resultElement = "";
			var form = ajax.getForm(arguments[0]);
			var URL = arguments[0];
			if(form) { //asumo que es un form.
				URL = form.action;
				query = ajax.getFormData(form);
				ajax.method = form.method.toUpperCase();
				if(ajax.method == "GET") {
					if(URL.indexOf("?") == -1)
						URL+="?"+query;
					else
						URL+="&"+query;
				}
				ajax.resultElement = arguments[1];
			}
			else if(arguments.length == 2) { //asumo que es por get.
				ajax.method = "GET";
				ajax.resultElement = arguments[1];
			}
			else if(arguments.length == 3) { //asumo que es por post.
				query = arguments[1];
				ajax.method = "POST";
				ajax.resultElement = arguments[2];
			}
			else {
				ajax.onError(1); //incorrect number of arguments
				return false;
			}
			ajax.xmlHttp = getXmlHttpObject();
			ajax.xmlHttp.onreadystatechange = ajax.onStateChange;
			if(ajax.xmlHttp) {
				ajax.isBusy = true;
				ajax.showLoadingElement();
				//open("method", "URL", [asyncFlag], ["userName"], ["password"])
				ajax.xmlHttp.open(ajax.method, URL, ajax.async, ajax.username, ajax.password);
				if(ajax.method == "POST") {
					ajax.xmlHttp.setRequestHeader("Method", "POST "+URL+" HTTP/1.1");
					ajax.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					ajax.xmlHttp.send(query);
				}
				else ajax.xmlHttp.send(null);
			}
			else
				return false;
			return true;
		}
	};
	
	ajax.getAllHeaders = function() {
		if(ajax.xmlHttp) {
			if(ajax.xmlHttp.readyState==4)
				return ajax.xmlHttp.getAllResponseHeaders();
			else
				ajax.onError(3);
		}
		else ajax.onError(0);
		return false;
	};
	ajax.getHeader = function(header) {
		if(ajax.xmlHttp) {
			if(ajax.xmlHttp.readyState==4)
				return ajax.xmlHttp.getResponseHeader(header);
			else
				ajax.onError(3);
		}
		else ajax.onError(0);
		return false;
	};
	ajax.setHeader = function(header, value) {
		if(ajax.xmlHttp) {
			if(ajax.xmlHttp.readyState==1) {
				ajax.xmlHttp.setRequestHeader(name, value);
				return true;
			}
			else
				ajax.onError(3);
		}
		else ajax.onError(0);
		return false;
	};
	
	ajax.toString = function(ver) {
		if(arguments.length == 0)
			return ajax.name + " - Method: " + ajax.method + " Asyncronous: " + ajax.async;
		return "AJAX Object v0.93\n\n\tby Ignacio X. Domínguez.";
	};
	
	/*******************\
	*  PRIVATE METHODS  *
	\*******************/
	ajax.getForm = function(form) {
		if(typeof(form) == "string")
			form=document.getElementById(form);
		if(form && form.tagName == "FORM") return form;
		return null;
	};
	
	ajax.getFormData = function(form) {
		data = "";
		if(form && form.tagName == "FORM") {
			for(i=0; i<form.elements.length; i++) {
				if(form.elements[i].disabled == false) {
					switch(form.elements[i].type) {
						case 'radio': case 'checkbox':
							if(form.elements[i].checked) data+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
							break;
						case 'hidden': case 'select-one': case 'text': case 'password': case 'textarea': case 'button': case 'submit': case 'reset':
							data+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
							break;
						case 'file':
							break;
						case 'select-multiple':
							for(j=0; j<form.elements[i].length; j++) {
								if(form.elements[i].options[j].selected)
									data+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].options[j].value)+"&";
							}
							break;
					}
				}
			}
		}
		return data;
	};
	
	ajax.showLoadingElement = function() {
		if(typeof(ajax.loadingElementID) == "string")
			loadingElement = document.getElementById(ajax.loadingElementID);
		else
			loadingElement = ajax.loadingElementID;
		if(loadingElement) {
			loadingElement.style.visibility = "visible";
			loadingElement.style.display = "block";
		}
	};
	
	ajax.hideLoadingElement = function() {
		if(typeof(ajax.loadingElementID) == "string")
			loadingElement = document.getElementById(ajax.loadingElementID);
		else
			loadingElement = ajax.loadingElementID;
		if(loadingElement) {
			loadingElement.style.visibility = "hidden";
			loadingElement.style.display = "none";
		}
	};
	
	return ajax;
}

Ajax.prototype = new Object();

/********************\
*  STATIC FUNCTIONS  *
\********************/

Ajax.prototype.doRequest = function(args) {
	tmpajax = new Ajax();
	tmpajax.doRequest(args);
};